﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.FinancialReport;
using S4Store.Models;
using S4Store.Service;

namespace S4Store.Controllers
{
    public class FinancialReportController : ReportBaseController
    {
        public FinancialReportService FinancialReportService { get; set; }
        public FinancialKpiService FinancialKpiService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
           
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                model.Report = FinancialReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult YYE()
        { 
            YYEModel model = new YYEModel();
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = FinancialReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                } 
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YYE";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("YyeHistory") + "' target='navtab' width='30%' title='营业额历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);

        }

        public ActionResult YyeHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            YyeHistoryModel model = new YyeHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                model.CarBrands = brands;
                foreach (var brand in brands)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = FinancialReportService.GetCarBrandReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "YyeHistory";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult ZJZZ()
        {
            ZJZZModel model = new ZJZZModel();
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = FinancialReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ZJZZ";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("ZjzzHistory") + "' target='navtab' width='30%' title='资金周转天数历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }


        public ActionResult ZjzzHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            ZjzzHistoryModel model = new ZjzzHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                model.CarBrands = brands;
                foreach (var brand in brands)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = FinancialReportService.GetCarBrandReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ZjzzHistory";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult SHYYE()
        {
            SHYYEModel model = new SHYYEModel();
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    var report = FinancialReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "SHYYE";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("ShyyeHistory") + "' target='navtab' width='30%' title='售后营业额历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);

        }


        public ActionResult ShyyeHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            ShyyeHistoryModel model = new ShyyeHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                model.CarBrands = brands;
                foreach (var brand in brands)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = FinancialReportService.GetCarBrandReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ShyyeHistory";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult FWXSL()
        {
            FWXSLModel model = new FWXSLModel();
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                t.Inject(AfterSaleCarKPIService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                model.StoreReport= FinancialReportService.GetStoreReport(Month.Year, Month.Month, Store.Id);
                //model.StoreReport.CarTypeReport = AfterSaleCarKPIService.GetCarTypeReport(Month.Year, Month.Month, Store.Id);
                foreach (var brand in brands)
                {
                    var report = FinancialReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    report.StoreReport = model.StoreReport;
                    //report.CarTypeReport = AfterSaleCarKPIService.GetCarTypeReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "FWXSL";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("FwxslHistory") + "' target='navtab' width='30%' title='服务吸收率历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }


        public ActionResult FwxslHistory()
        {
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            FwxslHistoryModel model = new FwxslHistoryModel();
            model.Start = Start.Value;
            model.End = End.Value;
            using (var t = new Transtion())
            {
                t.Inject(FinancialReportService);
                t.Inject(FinancialKpiService);
                var brands = FinancialKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                model.CarBrands = brands;
                foreach (var brand in brands)
                {
                    for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                    {
                        var report = FinancialReportService.GetCarBrandReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        model.Reports.Add(report);
                    }
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "FwxslHistory";
            model.BarModel.Controller = "FinancialReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }
    }
}
